<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { getDateArray, getPlayEndTime, getSummaries } from '@/utils'
  import { ElMessageBox } from 'element-plus';
  import whiteForm from './whiteForm.vue';
  import table2excel from 'js-table2excel';
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserid from '@/components/serachUserid/index.vue';
  const whiteFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const options = [
    {type:0,label:'否'},
    {type:1,label:'是'}
  ]
  const statusOptions = [
  { label: '未发放', value: 0 },
  { label: '已发放', value: 1 }
]
  const listQuery = reactive({
    page:1,
    limit:20,
    user_id:'',
    status:0,
  })
  const handleSerach = () => {
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const res = await propApi.colorWhiteList({
      ...listQuery,
      limit:action.total
    })
    loading.close()
    const { data=[] } = res?.data || {}
    const tHeader = [
      { title: '白名单ID', key: "id"},
      { title: '用户ID', key: "user_id"},
      { title: '用户UUID', key: "uuid"},
      { title: '用户昵称', key: "nickname"},
      { title: '彩蛋ID', key: "color_gift_id"},
      { title: '彩蛋名称', key: "name"},
      { title: '爆出礼物ID', key: "pop_gift_id"},
      { title: '爆出礼物名称', key: "pop_name"},
      { title: '是否从礼物池中扣除', key: "delete_pop_text"},
    ]
    const list = data.map((res:any)=>{
      const { delete_pop } = res
      return {
        ...res,
        delete_pop_text:options.find(res=>res.type == delete_pop)?.label || delete_pop
      }
    })
    table2excel(
      tHeader,
      list,
      `彩蛋白名单`)
  }
  const handleDel = (row:any = {})=>{
    const { nickname='', id=0 } = row
    ElMessageBox.confirm(`确定删除彩蛋白名单：${nickname}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await propApi.colorWhiteDel({
        id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await propApi.colorWhiteList(listQuery)
    loading.close()
    const { data=[], total=0 } = res?.data || {}
    action.list = data
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <div class="labelItem">
          <div class="label">发放状态：</div>
          <el-select v-model="listQuery.status" placeholder="请选择发放状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>

        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="primary" @click="whiteFormRef.openDialog()">新增白名单</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="白名单ID" />
      <el-table-column min-width="80" prop="user_id" label="用户ID" />
      <el-table-column min-width="80" prop="uuid" label="用户UUID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column min-width="80" prop="color_gift_id" label="彩蛋ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="name" label="彩蛋名称" />
      <el-table-column min-width="80" prop="pop_gift_id" label="爆出礼物ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="pop_name" label="爆出礼物名称" />
      <el-table-column min-width="80" prop="delete_pop" label="是否从礼物池中扣除">
        <template #default="scoped">
          {{ options.find(res=>res.type == scoped.row.delete_pop)?.label || scoped.row.delete_pop }}
        </template>
      </el-table-column>
      <el-table-column min-width="80" prop="status" label="发放状态">
        <template #default="scoped">
          {{ ['未发放','已发放'][scoped.row.status] || scoped.row.status }}
        </template>
      </el-table-column>
      <el-table-column width="160" prop="updated_at" label="发放时间" />
      <el-table-column width="180" fixed="right" label="操作">
        <template #default="scoped">
          <el-button v-if="scoped.row.status == 0" type="primary" @click="whiteFormRef.openDialog(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <whiteForm ref="whiteFormRef" @update-list="getList" />
  </div>
</template>
